coreEquiptAcqConfig.connectDetail = {
    getBaseInfo : function() {
        var _this = this;
        return {
            type : 'form',
            id : _this.idManager.limsEquiptAcqConfigForm,
            url : _this.getUrl('limsEquiptAcqConfigForm'),
            readonly : true,
            columns : 3,
            panels : [{
                title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_BASE_OPTION',
                id : _this.idManager.limsEquiptEditlistAcqConfigFormBasePanel,
                fields : coreEquiptAcqConfig.getAcqConfigBaseFields()
            }, {
                title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_TCP_OPTION',
                id : _this.idManager.limsEquiptEditlistAcqConfigFormTcpPanel,
                fields : coreEquiptAcqConfig.getAcqConfigTcpFields()
            }, {
                title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_SERIAL_OPTION',
                id : _this.idManager.limsEquiptEditlistAcqConfigFormSerialPanel,
                fields : coreEquiptAcqConfig.getAcqConfigSerialFields()
            }, {
                title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_FILE_OPTION',
                id : _this.idManager.limsEquiptEditlistAcqConfigFormFilePanel,
                fields : coreEquiptAcqConfig.getAcqConfigFileFields()
            }],
            service : 'coreEquiptAcqConfigService',
            onLoadSuccess : function(data) {
                _this.showAcqConfigFields();
            },
            onUpdated : function(data) {
                _this.showAcqConfigFields();
            }
        }
    },

    showAcqConfigFields : function() {
        var _this = this;
        Gikam.getAsyncComp(_this.idManager.limsEquiptAcqConfigForm).done(function(form) {
            var data = form.getData();
            if (data.acqCategory == 'net') {
                form.showPanelById([_this.idManager.limsEquiptEditlistAcqConfigFormTcpPanel]);
                form.hidePanelById([_this.idManager.limsEquiptEditlistAcqConfigFormSerialPanel, _this.idManager.limsEquiptEditlistAcqConfigFormFilePanel]);
            } else if (data.acqCategory == 'serial') {
                form.showPanelById([_this.idManager.limsEquiptEditlistAcqConfigFormSerialPanel]);
                form.hidePanelById([_this.idManager.limsEquiptEditlistAcqConfigFormTcpPanel, _this.idManager.limsEquiptEditlistAcqConfigFormFilePanel]);
            }
        });
    },

    /**
     * 消息历史
     */
    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : _this.idManager.limsEquiptAcqConfigSerialNetRecGrid,
            url : _this.getUrl('limsEquiptAcqConfigSerialNetRecGrid'),
            requestData : {
                equiptId_EQ : _this.param.id
            },
            page : true,
            columns : coreEquiptAcqConfig.getSerialNetColumns(),
            toolbar : [{
                type : 'form',
                refreshGrid : false,
                id : _this.idManager.connectStatusForm,
                readonly : true,
                fields : [{
                    field : 'connectStatus',
                    title : 'T_CORE_EQUIPT_ACQ_CONFIG.CONNECTSTATUS',
                    type : 'select',
                    category : 'equipt-connect-status',
                }]
            }, {
                type : 'button',
                text : 'CORE.MODULE.EQUIPT.ACQCONFIGS.BUTTON.CONNECT',
                icon : 'link',
                id : _this.idManager.limsEquiptAcqConfigConnectTestConnectButton,
                onClick : function() {
                    _this.connect();
                }
            }, {
                type : 'button',
                text : 'CORE.MODULE.EQUIPT.ACQCONFIGS.BUTTON.DISCONNECT',
                id : _this.idManager.limsEquiptAcqConfigConnectTestDisConnectButton,
                icon : 'disable',
                onClick : function() {
                    var serialListener = coreEquiptAcqConfig.serialListener;
                    var equipt = serialListener.equipt;
                    serialListener.clear();
                    Gikam.getComp(_this.idManager.connectStatusForm).setData({
                        connectStatus : ''
                    }, false);
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestConnectButton).show();
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestDisConnectButton).hide();
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestSendMessageButton).hide();
                    _this.disconnect();
                }
            }, {
                type : 'button',
                text : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.BUTTON.ACQ_CONFIG_SEND_MESSAGE',
                icon : 'submit', color: 'success',
                id : _this.idManager.limsEquiptAcqConfigConnectTestSendMessageButton,
                onClick : function() {
                    _this.sendMessage();
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.CLOSE',
                icon : 'cancel',
                onClick : function() {
                    coreEquiptAcqConfig.serialListener.clear();
                    Gikam.getLastModal().close();
                }
            }],
            onRendered : function() {
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestDisConnectButton).hide();
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestSendMessageButton).hide();
            }
        }
    },

    sendMessage : function() {
        var _this = this;
        var config = Gikam.getComp(_this.idManager.limsEquiptAcqConfigForm).getData();
        if (Gikam.isFalse(_this.validateData(config))) {
            return;
        }
        var simpleModal = Gikam.createSimpleModal({
            title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_SEND_MESSAGE',
            width : 360,
            height : 260,
            showMaxBtn : false,
            bodyStyle : {
                padding : "0 16px"
            },
            toolbarFormatter : function() {
                return [{
                    type : "button",
                    text : "GIKAM.BUTTON.CONFIRM",
                    class : "blue",
                    onClick : function() {
                        if (!form.validate()) {
                            return;
                        }
                        var url = '';
                        var messageData = form.getData();
                        if (Gikam.isNotEmpty(config.ext$.moduleipport)) {
                            Gikam.postText(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/action/test-send', {
                                id : config.id
                            }), Gikam.getJsonWrapper(null, ['', [messageData]])).done(function(result) {
                                Gikam.toast('send success');
                                Gikam.getLastModal().close();
                            });
                        } else {
                            if (config.acqCategory == 'net') {
                                url = Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{instId}/send', {
                                    instId : messageData.id,
                                })
                            }
                            if (config.acqCategory == 'serial') {
                                url = 'http://localhost:9778/serial-ports/' + config.com + '/send';
                            }
                            Gikam.postText(url, messageData).always(function(result) {
                                Gikam.toast('send success');
                                Gikam.getLastModal().close();
                            });
                        }
                    }
                }, {
                    type : "button",
                    text : "GIKAM.BUTTON.CLOSE",
                    class : "red",
                    onClick : function() {
                        Gikam.getLastModal().close()
                    }
                }];
            }
        });

        var form = Gikam.create('form', {
            columns : 1,
            id : _this.idManager.limsEquiptAcqSendMessageForm,
            titleWidth : 80,
            fields : [{
                field : 'id',
                title : 'T_LIMS_EQUIPT_ACQ_CONFIG_INST.INSTRUCTIONNAME',
                type : 'select',
                category : 'equipt-acq-instruction',
                param : {
                    acqConfigId_EQ : _this.param.id
                },
                onChange : function(field, value) {
                    var instructionFiled = this.getField(field);
                    var findItems = instructionFiled.items.filter(function(item) {
                        return item.id == value
                    });
                    var selectItem = findItems[0];
                    console.debug(selectItem);
                    this.setData({
                        id : value,
                        hixFlag : selectItem.hixFlag,
                        script : selectItem.script,
                        instruction : selectItem.instruction
                    });
                }
            }, {
                field : 'instruction',
                title : 'T_LIMS_EQUIPT_ACQ_CONFIG_INST.INSTRUCTION',
                type : 'textarea',
                height : 80,
                readonly : true,
                validators : ['notEmpty', 'strLength(0, 200)']
            }, {
                field : 'hixFlag',
                title : 'T_LIMS_EQUIPT_ACQ_CONFIG_INST.HIXFLAG',
                type : 'simpleCheckbox',
                readonly : true,
            }],
            renderTo : simpleModal.layout.options.center.$dom
        });
    },

    disconnect : function(){
        var _this = this;
        var equipt = Gikam.getComp(_this.idManager.limsEquiptAcqConfigForm).getData();
        if (Gikam.isFalse(_this.validateData(equipt))) {
            return ;
        }
        if (equipt.acqCategory == 'net' || equipt.acqCategory == 'mqtt') {
            Gikam.getLastModal().window.showMask(true);
             Gikam.put(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{eptId}/action/closeConnect', {
                eptId : equipt.id
            }), Gikam.getJsonWrapper(null, ['', []])).done(function (result) {
                Gikam.getLastModal().window.closeMask(true);
                Gikam.toast('close success');
            });
        }
    },

    connect : function() {
        var _this = this;
        var equipt = Gikam.getComp(_this.idManager.limsEquiptAcqConfigForm).getData();
        var action = _this.param.action;
        if (Gikam.isFalse(_this.validateData(equipt))) {
            return ;
        }
        var action = function(response, data, equipt) {
            Gikam.toast(data, 3, 'top');
            Gikam.getComp(_this.idManager.limsEquiptAcqConfigSerialNetRecGrid).refresh();
        };
        if (equipt.acqCategory == 'net' || equipt.acqCategory == 'mqtt') {
            Gikam.getLastModal().window.showMask(true);
            coreEquiptAcqConfig.serialListener.open('net', equipt, action).done(function() {
                Gikam.getComp(_this.idManager.connectStatusForm).setData({
                    connectStatus : 'connected'
                }, false);

                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestConnectButton).hide();
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestDisConnectButton).show();
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestSendMessageButton).show();
            });
        } else if (equipt.acqCategory == 'serial') {
            Gikam.getLastModal().window.showMask(true);
            coreEquiptAcqConfig.serialListener.open('serial', equipt, action, {
                baudRate : equipt.baudRate,
                dataBits : equipt.dataBit,
                stopBits : equipt.stopBit,
                parity : equipt.parityCheck,
                end : equipt.endFlag
            }).done(function() {
                Gikam.getComp(_this.idManager.connectStatusForm).setData({
                    connectStatus : 'connected'
                }, false);
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestConnectButton).hide();
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestDisConnectButton).show();
                Gikam.getComp(_this.idManager.limsEquiptAcqConfigConnectTestSendMessageButton).show();
            });
        }
    },

    validateData : function(equipt) {
        var _this = this;
        if (equipt.acqCategory == 'net') {

            if (Gikam.isEmpty(equipt.ip) || Gikam.isEmpty(equipt.port)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_IP_PORT');
                return false;
            }
        } else if (equipt.acqCategory == 'serial') {
            if (Gikam.isEmpty(equipt.com)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_COM');
                return false;
            }

            if (Gikam.isEmpty(equipt.baudRate)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_BAUDRATE');
                return false;
            }

            if (Gikam.isEmpty(equipt.dataBit)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_DATABIT');
                return false;
            }

            if (Gikam.isEmpty(equipt.stopBit)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_STOPBIT');
                return false;
            }

            if (Gikam.isEmpty(equipt.parityCheck)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_PARITYCHECK');
                return false;
            }
        }else if (equipt.acqCategory == 'mqtt') {
            if (Gikam.isEmpty(equipt.ip) || Gikam.isEmpty(equipt.port)) {
                Gikam.alert('CORE.MODULE.EQUIPT.ACQCONFIGS.TIP.NO_IP_PORT');
                return false;
            }
        }
        return true;
    },

    getUrl : function(key) {
        var _this = this;
        var id = _this.param.id;
        if (Gikam.isNotEmpty(id) && Gikam.isNotEmpty(_this.urlManager[key])) {
            return Gikam.printf(_this.urlManager[key], {
                id : id
            });
        } else {
            return '';
        }
    },

    urlManager : {
        limsEquiptAcqConfigForm : coreEquiptAcqConfig.baseUrl + '/{id}',
        limsEquiptAcqConfigSerialNetRecGrid : coreEquiptAcqConfig.serialNetRecUrl + '/queries/raw',
    },

    idManager : {
        limsEquiptAcqConfigForm : 'lims-equipt-connect-test-acq-config-form',
        limsEquiptAcqConfigSerialNetRecGrid : 'lims-equipt-connect-test-acq-config-serial-net-grid',
        connectStatusForm : 'lims-equipt-connect-test-acq-connect-status-form',
        limsEquiptAcqSendMessageForm : 'lims-equipt-connect-test-acq-connect-message-form',
        limsEquiptAcqConfigConnectTestConnectButton : 'lims-equipt-connect-test-connect-button',
        limsEquiptAcqConfigConnectTestDisConnectButton : 'lims-equipt-connect-test-disconnect-button',
        limsEquiptAcqConfigConnectTestSendMessageButton : 'lims-equipt-connect-test-send-message-button',
        limsEquiptEditlistAcqConfigFormBasePanel : 'lims-equipt-connect-test-acq-config-form-test-base-panel',
        limsEquiptEditlistAcqConfigFormTcpPanel : 'lims-equipt-connect-test-acq-config-form-test-tcp-panel',
        limsEquiptEditlistAcqConfigFormSerialPanel : 'lims-equipt-connect-test-acq-config-form-test-serial-panel',
        limsEquiptEditlistAcqConfigFormFilePanel : 'lims-equipt-connect-test-acq-config-form-test-file-panel',
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-equipt-acq-test-layout',
            renderTo : Gikam.getLastModal().window.$dom,
            north : {
                height : '240',
                items : [_this.getBaseInfo()]
            },
            center : {
                items : [_this.getGridParam()]
            }
        });
    },

    init : function(param) {
        var _this = this;
        _this.param = param;
        this.create();
    }
};
